package com.zzl.myblog.Utils;

import android.util.Log;

import java.util.Map;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

/**
 * 单例模式
 */
public class HttpUtil {
    private static String baseURL_MyBack = "http://192.168.43.207:3000/";
    private static String baseURL_wanandroid = "https://www.wanandroid.com/";

    String TAG = "HttpUtil";
    private static OkHttpClient client =new OkHttpClient();
    private static HttpUtil httpUtil ;

    private HttpUtil() {
        LogUtil.d(TAG, "验证是否单例生效,app使用过程中HttpUtil只初始化一次即可");
    }

    public static HttpUtil getInstance(){
        if (httpUtil!=null){
            return httpUtil;
        }else {
            return new HttpUtil();
        }
    }


    /**
     * 工具类 GET
     */
    private static void GET_Proxy(String baseURL,String url,String params,okhttp3.Callback callback){
        Request request = new Request.Builder()
                .url(baseURL+url+params)
                .build();
        client.newCall(request).enqueue(callback);
    }

    /**
     * 工具类 POST
     */
    private static void POST_Proxy(String baseURL,String url, RequestBody body, okhttp3.Callback callback){
        Request request = new Request.Builder()
                .url(baseURL+url)
                .post(body)
                .build();
        client.newCall(request).enqueue(callback);
    }


    /**
     * 用户登录【GET】
     */
    public static void user_GET(String url, String params, okhttp3.Callback callback){
        GET_Proxy(baseURL_MyBack,url,params,callback);
    }

    /**
     * 用户注册【POST】
     */
    public static void user_register_POST(String url, Map<String,String> map, okhttp3.Callback callback){
        RequestBody body = new FormBody.Builder()
                .add("username",map.get("username"))
                .add("password",map.get("password"))
                .build();
        POST_Proxy(baseURL_MyBack,url,body, callback);
    }

    /**
     * 用户信息更改
     */
    public static void user_update_POST(String url, Map<String,String> map, okhttp3.Callback callback){
        RequestBody body = new FormBody.Builder()
                .add("oldUsername",map.get("oldUsername"))
                .add("username",map.get("username"))
                .add("sex",map.get("sex"))
                .add("age",map.get("age"))
                .build();
        POST_Proxy(baseURL_MyBack,url,body, callback);
    }

    /**
     * wanandroid API
     * @param url
     * @param callback
     */
    public static void wanAndroid_GET(String url,String params,okhttp3.Callback callback){
        GET_Proxy(baseURL_wanandroid,url,params,callback);
    }

    /**
     * 获取某个用户的所有收藏文章[GET]
     */
    public static void collect_GET(String url,String params,okhttp3.Callback callback){
        GET_Proxy(baseURL_MyBack,url,params,callback);
    }

    /**
     * 某个用户-添加文章[POST]
     */
    public static void collect_POST(String url, Map<String,String> map,okhttp3.Callback callback){
        RequestBody body = new FormBody.Builder()
                .add("collectUrl",map.get("collectUrl"))
                .add("nickname",map.get("nickname"))
                .add("collectTitles",map.get("collectTitles"))
                .add("username",map.get("username"))
                .build();
        POST_Proxy(baseURL_MyBack,url,body,callback);
    }

    /**
     * 某个用户-删除文章[DELETE]
     */
    public static void collect_DELETE(String url,String params,okhttp3.Callback callback){
        GET_Proxy(baseURL_MyBack,url,params,callback);
    }
}
